package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.LotDefectDataAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.DefectCodeBean;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.bean.ResponseQueryBean;
import com.glory.wxelectricitypda.bean.ScrapBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class LotDefectActivity extends BaseDialogActivity {

    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.rvDefectList)
    RecyclerView rvDefectList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flDefectList)
    FrameLayout flDefectList;
    @BindView(R.id.tvDefect)
    TextView tvDefect;
    @BindView(R.id.ivDefect)
    ImageView ivDefect;
    @BindView(R.id.tvStepName)
    TextView tvStepName;
    @BindView(R.id.tvWoID)
    TextView tvWoID;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    @BindView(R.id.tvLotQty)
    TextView tvLotQty;
    @BindView(R.id.tvPartName)
    TextView tvPartName;

    private LotDefectDataAdapter mLotDefectDataAdapter;
    private FreeUI_GeneralPop mPositionPop;
    private ArrayList<String> defectList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_defect_lot;
    }

    @Override
    protected void initView() {
        rvDefectList.setLayoutManager(new LinearLayoutManager(LotDefectActivity.this));
        rvDefectList.setItemAnimator(new DefaultItemAnimator());
        rvDefectList.addItemDecoration(new DividerItemDecoration(LotDefectActivity.this, 1));
        mLotDefectDataAdapter = new LotDefectDataAdapter(null);
        mLotDefectDataAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mLotDefectDataAdapter.isFirstOnly(true);
        rvDefectList.setAdapter(mLotDefectDataAdapter);
    }

    @Override
    protected void initData() {
        freeUI_titleView.setTitle("批次报废");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View s) {
                if (etLotId.getText().toString().equals("")){
                    showToast("请扫描批次号",false);
                    return;
                }
                JSONObject jsonObject = new JSONObject();
                JSONObject jsonObject1 = new JSONObject();
                try {
                    jsonObject1.put("ACTIONTYPE", "SCRAP");
                    JSONArray jsonArray = new JSONArray(new Gson().toJson(mLotDefectDataAdapter.getData()));
                    jsonObject1.put("LOTACTIONLIST", jsonArray);
                    jsonObject1.put("LOT", new JSONObject(new Gson().toJson(new LotBean(etLotId.getText().toString()))));
                    jsonObject.put("Data",jsonObject1);
                    jsonObject.put("MessageName","EUIT.EUITLOTSCRAP");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                        new TypeToken<ResponseBean<Object>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<Object>> response) {
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("提交成功",true);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            },2000);
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<Object>> response) {
                        super.onError(response);
                        loadError(response.getException(), "EUITLOTSCRAP");
                    }
                });
            }
        });

        requestCode();
        initEdit(etLotId,s -> {
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject1 = new JSONObject();
            try {
                jsonObject1.put("LOTID", s);
                jsonObject1.put("ACTIONTYPE", "GETLOTINFO");
                jsonObject.put("Data",jsonObject1);
                jsonObject.put("MessageName","EUIT.EUITLOTQUERY");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotBean>>(
                    new TypeToken<ResponseBean<LotBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {
                @Override
                public void onSuccess(Response<ResponseBean<LotBean>> response) {
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        tvStepName.setText(response.body().getBody().getLOT().getSTEPNAME());
                        tvLotQty.setText(String.valueOf(response.body().getBody().getLOT().getMAINQTY()));
                        tvStatus.setText(response.body().getBody().getLOT().getSTATE());
                        tvWoID.setText(response.body().getBody().getLOT().getWOID());
                        tvPartName.setText(response.body().getBody().getLOT().getPARTNAME());
                        getScrapcodeList(s);
                    }
                }

                @Override
                public void onError(Response<ResponseBean<LotBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "GETLOTINFO");
                }
            });
        });

    }

    private void getScrapcodeList(String lotId) {
        JSONObject map = new JSONObject();
        try {
            map.put("LotID", lotId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getScrapcodeList","GetScrapcodeList",map,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotBean>>(
                new TypeToken<ResponseBean<LotBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<LotBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    for (LotBean.ScrapcoDescBean scrapcode : response.body().getBody().getSCRAPCODES()) {
                        defectList.add(scrapcode.getCODE()+"-"+scrapcode.getDESCRIPTION());
                    }
                }
            }
            @Override
            public void onError(Response<ResponseBean<LotBean>> response) {
                super.onError(response);
                loadError(response.getException(), "GetScrapcodeList");
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("批次报废");
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.flDefectList,R.id.tvAddDefect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvAddDefect:
                if (tvDefect.getText().toString().equals("")){
                    showToast("请选择不良代码",false);
                    return;
                }
                DefectCodeBean defectCodeBean = new DefectCodeBean();
                defectCodeBean.setACTIONTYPE("SCRAP");
                defectCodeBean.setACTIONCODE(tvDefect.getText().toString().split("-")[0]);
                defectCodeBean.setLOTID(etLotId.getText().toString());
                defectCodeBean.setACTIONDESC(tvDefect.getText().toString().split("-")[1]);
                mLotDefectDataAdapter.addData(defectCodeBean);
                break;
            case R.id.flDefectList:
                if (mPositionPop != null) {
                    if (mPositionPop.isShowing()) {
                        mPositionPop.dismiss();
                    }
                } else {
                    mPositionPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flDefectList.getWidth(), defectList, ivDefect, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {
                            tvDefect.setText(s);
                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivDefect);
                mPositionPop.showAsDropDown(flDefectList, 0, 0);
                break;
        }
    }

    private void requestCode() {
        defectList.clear();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("ACTIONTYPE", "BYUREFLIST");
            jsonObject1.put("ACTIONCODE", "ShipCode");
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITSCRAPCODE");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<ResponseQueryBean>>(
                new TypeToken<ResponseBean<ResponseQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ResponseQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    for (ScrapBean scrapBean : response.body().getBody().getSCRAPCODES()) {
                        defectList.add(scrapBean.getCODE() + "-" + scrapBean.getDESCRIPTION());
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<ResponseQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "入库码请求");
            }
        });
    }
}

